package org.springboot3.project.web.listener;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

@Slf4j
@Component
public class ServerWebNacosListener implements CommandLineRunner {

    @Autowired
    private ConfigService configService;


    @Override
    public void run(String... args) throws Exception {
        String dataId = "test";
        String group = "test";
        Listener listener = new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                log.info(">> receive config info: {}", configInfo);
            }
        };
        String configAndSignListener = configService.getConfigAndSignListener(dataId, group, 3000, listener);
        log.info("* Listener added. dataId={}，group={}, config={}", dataId, group, configAndSignListener);
    }
}
